package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;

import ve.com.wayoyo.wuml_case.Menu_Principal;
import ve.com.wayoyo.wuml_case.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

public class EliminarDiagrama extends Dialog {

	private String _proyecto;
	private ArrayList<CheckBox> _checkList;
	private CheckBox _cbSelectAll;
	private Context _context;
	private int _cant;

	public EliminarDiagrama(Context context, String proyecto) {
		super(context);
		this._context = context;
		this._proyecto = proyecto;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.eliminar_diagrama);
		setCanceledOnTouchOutside(true);
		setTitle("Indique el diagrama que desea eliminar");
		_checkList = new ArrayList<CheckBox>();
		_cbSelectAll = (CheckBox) findViewById(R.id.todos_diagramas);
		setCbFunct();
		cargaListaCheck();
		setOkFunct();
		setCancellFunct();

	}

	private void setCancellFunct() {
		Button bt_cancell = (Button) findViewById(R.id.bt_cancelar);
		bt_cancell.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				EliminarDiagrama.this.dismiss();
			}
		});
	}

	private void setOkFunct() {
		Button bt_Ok = (Button) findViewById(R.id.bt_aceptar);
		bt_Ok.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Confirmacion diag = new Confirmacion(getContext(),
						EliminarDiagrama.this, EliminarDiagrama.this._cant);
				diag.show();
			}
		});
	}

	public void eliminar() {
		File mPath;
		for (CheckBox check : _checkList) {
			if (check.isChecked()) {
				// Alert.show(Environment.getExternalStorageDirectory() +
				// "//WUML//"+EliminarDiagrama.this._proyecto+"//DIAGRAMAS//"+check.getText(),
				// this,Alert.ERROR);
				mPath = new File(Environment.getExternalStorageDirectory()
						+ "//WUML//" + EliminarDiagrama.this._proyecto
						+ "//DIAGRAMAS//" + check.getText());
				mPath.delete();
			}
		}
		((Menu_Principal) this._context).resetspListFiles(_proyecto);
		this.dismiss();
	}

	private void setCbFunct() {
		_cbSelectAll.setOnClickListener(new CheckBox.OnClickListener() {
			public void onClick(View v) {
				if (_cbSelectAll.isChecked()) {
					EliminarDiagrama.this.seleccionarTodos();
				} else {
					EliminarDiagrama.this.deseleccionarTodos();
				}
			}
		});
	}

	protected void deseleccionarTodos() {
		for (CheckBox check : _checkList) {
			check.setChecked(false);
		}
		_cant = 0;
	}

	protected void seleccionarTodos() {
		int count = 0;
		for (CheckBox check : _checkList) {
			check.setChecked(true);
			count++;
		}
		_cant = count;
	}

	private void cargaListaCheck() {
		String[] list = cargaListaDiagramas(_proyecto);
		LinearLayout layout_diag = (LinearLayout) findViewById(R.id.ly_diagrama_list);
		layout_diag.removeAllViewsInLayout();
		for (String value : list) {
			CheckBox cb = new CheckBox(getContext());
			cb.setText(value);
			layout_diag.addView(cb);
			_checkList.add(cb);
			cb.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					if (isChecked) {
						EliminarDiagrama.this.isAllChecked();
						EliminarDiagrama.this._cant = EliminarDiagrama.this._cant + 1;
					} else {
						_cbSelectAll.setChecked(false);
						EliminarDiagrama.this._cant = EliminarDiagrama.this._cant - 1;
					}
				}
			});
		}
	}

	protected void isAllChecked() {
		Boolean flag = true;
		for (CheckBox check : _checkList) {
			if (!(check.isChecked())) {
				flag = false;
			}
		}
		if (flag == true) {
			this.seleccionarTodos();
			_cbSelectAll.setChecked(true);
		}
	}

	private String[] cargaListaDiagramas(String proyecto) {
		String[] values;
		File mPath = new File(Environment.getExternalStorageDirectory()
				+ "//WUML//" + proyecto + "//DIAGRAMAS");
		try {
			mPath.mkdirs();
		} catch (SecurityException e) {
			// Log.e(TAG, "unable to write on the sd card " + e.toString());
		}
		if (mPath.exists()) {
			FilenameFilter filter = new FilenameFilter() {
				public boolean accept(File dir, String filename) {
					File sel = new File(dir, filename);
					return (filename.contains(".wuml"));
					// return true;
				}
			};
			values = mPath.list(filter);
		} else {

			values = new String[0];
		}
		return values;
	}

}
